package com.example.front.controller;

import com.example.common.dto.JsonResult;
import com.example.common.dto.ListDto;
import com.example.common.dto.Pagination;
import com.example.front.dto.ButtonCreateDto;
import com.example.front.dto.ButtonDto;
import com.example.front.dto.ButtonSearchDto;
import com.example.front.entity.Button;
import com.example.front.serivce.ButtonService;
import com.example.interceptor.UserContext;
import com.example.passport.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api(tags = "按钮管理")
@RestController
@CrossOrigin
@RequestMapping("/front/button")
public class ButtonController {
    @Resource
    ButtonService buttonService;

    @ApiOperation("添加按钮")
    @PostMapping("/create")
    public JsonResult<String> create(@Valid ButtonCreateDto buttonCreateDto){
        if(buttonCreateDto.getKey()<=0){
            return new JsonResult<>(false,"请输入合理的key");
        }
        User user = UserContext.getUser();
        Button button = new Button();
        BeanUtils.copyProperties(buttonCreateDto,button);
        button.setTenantId(user.getTenantId());
        buttonService.createButton(button);
        return new JsonResult<>(true, "添加成功");
    }

    @ApiOperation("删除按钮")
    @PostMapping("/delete")
    public JsonResult<String> delete(Long id){
        if(id==null || id <= 0){
            return new JsonResult<>(false,"请输入按钮id");
        }
        User user = UserContext.getUser();
        Button button = Button.builder().id(id).tenantId(user.getTenantId()).build();
        buttonService.deleteButton(button);
        return new JsonResult<>(true, "删除成功");
    }

    @ApiOperation("列出按钮")
    @GetMapping("/list")
    public JsonResult<ListDto<ButtonDto>> list(ButtonSearchDto condition, Pagination pagination){
        User user = UserContext.getUser();
        condition.setTenantId(user.getTenantId());

        ListDto<ButtonDto> roleDtoList = buttonService.list(condition,pagination);
        return new JsonResult<>(roleDtoList);
    }

    @ApiOperation("修改按钮")
    @PostMapping("/update")
    public JsonResult<String> update(@Valid ButtonDto buttonDto){
        User user = UserContext.getUser();
        Button button = new Button();
        BeanUtils.copyProperties(buttonDto,button);
        button.setTenantId(user.getTenantId());

        buttonService.update(button);
        return new JsonResult<>(true, "修改成功");
    }

}
